package com.spring.oaassist.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.spring.common.web.dao.TracelogboMapper;
import com.spring.common.web.model.TracelogboWithBLOBs;
import com.spring.oaassist.web.dao.CourseClassBabyMapper;
import com.spring.oaassist.web.dao.CourseClassMapper;
import com.spring.oaassist.web.model.CourseClass;
import com.spring.oaassist.web.model.CourseClassBaby;
import com.spring.oaassist.web.service.UserClassManageService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.SsoMapper;

@Service
public class UserClassManageServiceImpl implements UserClassManageService{

	@Resource CourseClassMapper courseClassMapper;
	
	@Resource CourseClassBabyMapper courseClassBabyMapper;
	
	@Resource TracelogboMapper tracelogboMapper;
	
	@Resource SsoMapper ssoMapper;

	@Override
	public List<CourseClass> getCourseClass(CourseClass parm) {
		CourseClass c = new CourseClass();
		c.setClassName("...");
		List<CourseClass> ret = courseClassMapper.selectByParm(parm);
		ret.add(0, c);
		return courseClassMapper.selectByParm(parm);
	}

	@Override
	public List<Map<String, Object>> classUserList(Map<String, Object> parm) {
		return courseClassMapper.classUserList(parm);
	}

	@Override
	public BaseResult deleteCourseClassUser(String ids,HttpServletRequest request) {
		BaseResult ret = new BaseResult();
		if(ApplicationUtils.isNotNull(ids)){
			String[] idArr = ids.split(",");
			List<String> idList = new ArrayList<String>();
			for(String id:idArr){
				idList.add(id);
				
				TracelogboWithBLOBs traclelogba = MyTools.getOaLogByRequest(request);
				traclelogba.setEntitytypename("course_class_baby");
				traclelogba.setEntityid(id);
				traclelogba.setLogcategory(3);
				traclelogba.setLogdescription("删除:id["+id+"]");
				int effect=tracelogboMapper.insert(traclelogba);
				System.out.println(effect);
			}	
			courseClassBabyMapper.deleteCourseClassUser(idList);			
		}else{
			ret.setStatus("0").setMsg("请先选择课程学生");
		}
		return ret;
	}

	@Override
	public BaseResult addCourseClassUser(List<CourseClassBaby> data,HttpServletRequest request) throws Exception {
		BaseResult ret = new BaseResult();
		Map<String,Object> parm = new HashMap<String,Object>();
		List<String> ids = new ArrayList<String>();
		for(CourseClassBaby one:data){
			ids.add(one.getBabyId());
		}
		if(ids.size()>0){
			parm.put("userIds", ids);
			List<Map<String,Object>> retL = courseClassMapper.classUserList(parm);
			if(retL.size()>0){
				String retMsg = "";
				for(Map<String,Object> m:retL){
					retMsg += m.get("baby_name")+",";
				}
				ret.setStatus("0").setMsg(retMsg.substring(0,retMsg.length()-1)+"已经报名该课程，本次操作失败");
				return ret;
			}
		}
		if(data!=null&&data.size()>0){
			//初始化时间等系统信息
			for(CourseClassBaby one:data){
				one.setId(ApplicationUtils.randomUUID());
				one.setEnterDate(new Date());
				one.initInfo();
				
				TracelogboWithBLOBs traclelogba = MyTools.getOaLogByRequest(request);
				traclelogba.setEntitytypename("t_teacher");
				traclelogba.setEntityid(one.getId());
				traclelogba.setLogcategory(1);
				traclelogba.setLogdescription(MyTools.getChangeByObject(one, "新增"));
				int effect=tracelogboMapper.insert(traclelogba);
				System.out.println(effect);
			}
			courseClassBabyMapper.addCourseClassUser(data);
		}else{
			ret.setStatus("0").setMsg("请先选择课程学生");
		}
		return ret;
	}

	@Override
	public List<Map<String, Object>> ssoUserList(Map<String, Object> parm) {
		return ssoMapper.custCourseApplyUserList(parm);
	}
}
